#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  03-dirControl-feature.sh
# Version	:  1.0
# Date		:  2020/06/01
# Author	:  Lz
# Email		:  lz843723683@gmail.com
# History	:     
#                  Version 1.0, 2020/06/01
# Function	:  测试三权分立 - 目录访问控制策略
# Out		:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="三权分立(系统特性) - 目录访问控制策略"

HeadFile_Source_LTFLIB="${LIB_SSHAUTO}"

testuser1_dirControl="ltfdirControl"
passwd1_dirControl="olleH717.12.#$"
userip_dirControl="localhost"
AddUserNames_LTFLIB="${testuser1_dirControl}"
AddUserPasswds_LTFLIB="${passwd1_dirControl}"

## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB(){
        # 配置免密登录
        SshAuto_OneConfig_LTFLIB "${userip_dirControl}" "${testuser1_dirControl}" "${passwd1_dirControl}"
        TestRetParse_LTFLIB "配置免密登录" "True" "no" "yes"

        SshAuto_SetIpUser_LTFLIB "${userip_dirControl}" "${testuser1_dirControl}"
        TestRetParse_LTFLIB "设置默认IP和用户名" "True" "no" "yes"

	return ${TPASS}
}


## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB(){
	return ${TPASS}
}


## TODO ：使用secadm用户登录系统，创建目录/tmp/dir
testcase_1(){
	local tmpdir_dirControl="${TmpTestDir_LTFLIB}/ltf_dircontrol_sopft"
	local tmpuser_dirControl="$testuser1_dirControl"

	if [ -d "$tmpdir_dirControl" ];then
		rm -rf $tmpdir_dirControl
	fi

	# 创建目录
	SshAuto_CmdLocalSec_LTFLIB "mkdir $tmpdir_dirControl" "no" "no"
        TestRetParse_LTFLIB "secadm 目录 $tmpdir_dirControl 创建成功" "False"

	# 普通用户进入目录
	SshAuto_CmdDef_LTFLIB "cd $tmpdir_dirControl;pwd" "no" "no"
	SshAuto_CmdDef_LTFLIB "cd $tmpdir_dirControl" "no" "no"
        TestRetParse_LTFLIB "普通用户 $tmpuser_dirControl 可以进入目录 $tmpdir_dirControl" "False"

	# 使用secadm用户配置用户对目录去除权限
	local autority="---"
	SshAuto_CmdLocalSec_LTFLIB "sudo setfacl -m u:${tmpuser_dirControl}:${autority} ${tmpdir_dirControl}" "no" "no"
        TestRetParse_LTFLIB "设置普通用户 $tmpuser_dirControl 对目录 ${tmpdir_dirControl} 权限为 ${autority}" "False"

	# 查看目录权限
	SshAuto_CmdLocalSec_LTFLIB "sudo getfacl $tmpdir_dirControl | grep \"user:${tmpuser_dirControl}:${autority}\"" "no" "no"
        TestRetParse_LTFLIB "当前普通用户 $tmpuser_dirControl 对目录 ${tmpdir_dirControl}权限为 ${autority}" "False"

	# 普通用户进入目录
	SshAuto_CmdDef_LTFLIB "cd $tmpdir_dirControl" "no" "yes"
        TestRetParse_LTFLIB "普通用户 $tmpuser_dirControl 无法进入目录 $tmpdir_dirControl" "False"

	# 清除目录
	if [ -d "$tmpdir_dirControl" ];then
		SshAuto_CmdLocalSec_LTFLIB "rm -rf $tmpdir_dirControl" "yes" "no"
	fi
}


## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB(){
	testcase_1

	return $TPASS
}


#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
